package org.xiyinjihe.platform.dto.resp;

import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public abstract class BaseDataRespDTO<T> extends BaseRespDTO {

    private T data;

    static {
        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                if (value != null && !("".equals(value.toString()))) {
                    if(value instanceof Long) {
                        return new Date((long) value);
                    } else if(value instanceof Date) {
                        return value;
                    }
                }
                return null;
            }
        }, Date.class);

        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                if (value != null && !("".equals(value.toString()))) {
                    return new BigDecimal(value.toString());
                }
                return null;
            }
        }, BigDecimal.class);

        ConvertUtils.register(new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                try {
                    if (value == null){
                        return null;
                    }
                    PageInfo obj = (PageInfo)type.newInstance();
                    BeanUtils.populate(obj, (Map) value);
                    return obj;

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, PageInfo.class);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public T getEntity(Class<T> clazz) {
        return convert(data, clazz);
    }

    public BigDecimal getMoneyEntity(Class<T> clazz) {
        if (this.getData() instanceof BigDecimal) {
            return new BigDecimal(this.getData().toString());
        } else {
            return BigDecimal.ZERO;
        }
    }

    public List<T> getListEntity(Class<T> clazz) {
        if (this.getData() instanceof List) {
            List list = (List) this.getData();
            List<T> returnList = new ArrayList<>(list.size());
            for (int i = 0; i < list.size(); i++) {
                T convertDate = convert((T) list.get(i), clazz);
                returnList.add(convertDate);
            }
            return returnList;
        } else {
            throw new RuntimeException("类型不匹配");
        }
    }

    protected T convert(Object data, Class<T> clazz) {
        try {
            if (data == null) {
                return null;
            } else if (data instanceof Map) {
                T obj = clazz.newInstance();
                BeanUtils.populate(obj, (Map) data);
                return obj;
            } else if(data instanceof Double){
                String clazzName = clazz.getSimpleName();
                if(clazzName.equalsIgnoreCase("BigDecimal")){
                    return (T)new BigDecimal(data.toString());
                }else if(clazzName.equalsIgnoreCase("Double")){
                    return (T)new Double(data.toString());
                }
            } else if(data instanceof Boolean){
                String clazzName = clazz.getSimpleName();
                    return (T)(Boolean) data;
            } else if(data instanceof Number){
                String clazzName = clazz.getSimpleName();
                if(clazzName.equalsIgnoreCase("BigDecimal")){
                    return (T)new BigDecimal(data.toString());
                }else if(clazzName.equalsIgnoreCase("Integer")){
                    return (T)new Integer(data.toString());
                }
            } else {
                T obj = clazz.newInstance();
                BeanUtils.populate(obj, (Map) data);
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
